/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.unam.aragon.controller;

import java.io.Serializable;
import java.util.List;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import mx.unam.aragon.domain.Alumno;
import mx.unam.aragon.repository.AlumnoRepository;
import org.springframework.stereotype.Component;

/**
 * @author erico
 */
@Component(value = "alumnoController")
@ManagedBean(name = "alumnoController")
@ViewScoped
public class AlumnoController implements Serializable {

    private static final int PAGE_SIZE = 10;
    private AlumnoRepository alumnoRepository;
    private Alumno alumno = new Alumno();
    private boolean saved;
    private int page;    
    private int index = -1;

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
    
    public Alumno getAlumno() {
        return alumno;
    }

    public void setAlumno(Alumno alumno) {
        this.alumno = alumno;
    }

    public void validate() {
    }

    public void save() {
        alumnoRepository.save(alumno);
        saved = true;
        setAlumno(new Alumno());
    }

    public void cancelar() {        
        setIndex(-1);
        setAlumno(new Alumno());        
    }

    public void selected(){
        System.out.println("SELECTED ");
    }

    public void editar() {        
        System.out.println("Alumno: " + alumno.getId());
    }

    public void modificar() {        
        alumnoRepository.merge(alumno);        
        setIndex(-1);
        setAlumno(new Alumno());
    }

    @Inject
    public void setAlumnoRepository(AlumnoRepository alumnoRepository) {
        this.alumnoRepository = alumnoRepository;
    }

    public boolean isSaved() {
        return saved;
    }

    public List<Alumno> getAlumnos() {
        return alumnoRepository.findAll(page, PAGE_SIZE);

    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public static <T> T getManagedBean(String beanName, Class<T> clazz) {
        final FacesContext fc = FacesContext.getCurrentInstance();
        final ELContext elc = fc.getELContext();
        final ExpressionFactory ef = fc.getApplication().getExpressionFactory();
        final String valueExpressionString = prepareValueExpression(beanName);
        final ValueExpression ve = ef.createValueExpression(elc, valueExpressionString, clazz);
        return (T) ve.getValue(elc);
    }

    private static String prepareValueExpression(String probableValueExpressionString) {
        final String valueExpressionString = probableValueExpressionString.startsWith("#{") && probableValueExpressionString.endsWith("}") ? probableValueExpressionString : "#{" + probableValueExpressionString + "}";
        return valueExpressionString;
    }
}
